/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package asfabdesk.dominio;

/**
 *
 * @author Marco Aurélio
 */
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.joda.time.DateTime;

public class Data {

    public Data() {
        dataFormatada = new SimpleDateFormat("dd/MM/yyyy");
        dataFormatadaParaBanco = new SimpleDateFormat("dd/MM/yyyy");
        horaFormatada = new SimpleDateFormat("HH:mm");
        dataHoraFormatadaISO = new SimpleDateFormat("yyyy/MM/dd HH:mm s");
        dataHoraFormatadaBra = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    }

    public java.util.Date ConverteDataStringDate(String data)
            throws ParseException {
        java.util.Date dataDate = dataFormatada.parse(data);
        return dataDate;
    }

    public java.util.Date ConverteDataHoraStringDate(String data)
            throws ParseException {
        java.util.Date dataDate = dataFormatada.parse(data);
        return dataDate;
    }

    public String ConverteDataDateStringBra(java.util.Date data) {
        if (data != null) {
            String formatada = dataFormatada.format(data);
            return formatada;
        } else {
            return null;
        }
    }

    public String ConverteDataDateTimeStringBra(DateTime data) {
        if (data != null) {
            String formatada = dataFormatada.format(data);
            return formatada;
        } else {
            return null;
        }
    }

    public String ConverteDataHoraDateStringBra(java.util.Date data) {
        if (data != null) {
            String formatada = dataHoraFormatadaBra.format(data);
            return formatada;
        } else {
            return null;
        }
    }

    public Calendar ConverteDataStringParaCalendar(String data) {
        try {
            Calendar cal = new GregorianCalendar();
            cal.setTime(dataFormatada.parse(data));
            return cal;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Calendar ConverteDataHoraStringParaCalendar(String data) {
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(dataHoraFormatadaBra.parse(data));
            return cal;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String ConverteDataCalendarParaString(Calendar data) {
        try {
            String dataString = dataFormatada.format(data);
            return dataString;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public String ConverteDataHoraCalendarParaString(Calendar cal) {
        if (cal != null) {
            try {
                String dataString = cal.get(Calendar.DATE) + "/" + cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.YEAR)
                        + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND);
                return dataString;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public java.util.Date ConverteDataStringDateParaBanco(String data)
            throws ParseException {
        int dia;
        int mes;
        int ano;
        dia = Integer.parseInt(data.substring(0, 2));
        mes = Integer.parseInt(data.substring(3, 5));
        ano = Integer.parseInt(data.substring(6, 10));
        return Date.valueOf((new StringBuilder()).append(ano).append("-").append(mes).append("-").append(dia).toString());
        //NumberFormatException e)
        //e;
        //return null;
        //e;
        //e.printStackTrace();
        //System.out.print("Erro aquiiiiiiiiiiiiiiiiiiii ************************************************###################################");
        //return null;
    }
    DateFormat dataFormatada;
    DateFormat dataFormatadaParaBanco;
    DateFormat horaFormatada;
    DateFormat dataHoraFormatadaISO;
    DateFormat dataHoraFormatadaBra;
}
